home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Utils / Forecastfox / Bin / forecastfox-0.8.5.1-fx+mz+ns.xpi / components / ffPing.js < prev    next >
Encoding:
Text File  |  2006-02-18  |  11.2 KB  |  318 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Forecastfox.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Jon Stritar <jstritar@MIT.EDU>.
  18.  * Portions created by the Initial Developer are Copyright (C) 2005
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  * Jon Stritar <jstritar@MIT.EDU>
  23.  * Richard Klein <richwklein@mchsi.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. const CLASS_ID = Components.ID("{71B45B67-5AC7-4aaf-8043-A3DD45C67636}");
  40. const CLASS_NAME = "Forecastfox Ping Component";
  41. const CONTRACT_ID = "@ensolis.com/forecastfox/ping;1";
  42. const ffIPing = Components.interfaces.ffIPing;
  43.  
  44. /******************************************************************************
  45.  * ffPing Component
  46.  ******************************************************************************/
  47. function ffPing() {};
  48. ffPing.prototype = {
  49.   _URL: "http://forecastfox.ensolis.com/logs/log.php",
  50.   _manager: null,
  51.   _ds: null,
  52.   _obs: null,
  53.   _downgrad: false,
  54.   get url() { return this._URL; },
  55.   
  56.   start: function()
  57.   {
  58.     this._manager = Components.classes["@ensolis.com/forecastfox/manager;1"].getService(Components.interfaces.ffIManager); 
  59.     
  60.     //figure out if we are toolkit or mozilla
  61.     var isToolkit = true;
  62.     var isDeerPark = true;
  63.     try {    
  64.       var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
  65.     } catch(e) {
  66.       isToolkit = false;
  67.     };
  68.     
  69.     //toolkit so add observers
  70.     if (isToolkit) {
  71.       try {
  72.         var test = em.getItemForID("{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}");
  73.       } catch(e) {
  74.         isDeerPark = false;
  75.       };
  76.       
  77.       if (isDeerPark) {
  78.         this._obs = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);        
  79.         this._obs.addObserver(this, "em-action-requested", false);
  80.       } else {
  81.         this._ds = em.datasource;
  82.         this._ds.AddObserver(this);
  83.       };      
  84.     };
  85.   },
  86.   
  87.   stop: function()
  88.   {
  89.     //unregister as an observer of the em datasource  
  90.     if (this._ds)
  91.       this._ds.RemoveObserver(this);
  92.     
  93.     if (this._obs) 
  94.       this._obs.removeObserver(this, "em-action-requested");
  95.     
  96.     //remove variables
  97.     this._manager = null;
  98.     this._ds = null;
  99.     this._obs = null;
  100.     this._downgrade = null;
  101.   },
  102.   
  103.   open: function()
  104.   {
  105.     //get pref branch
  106.     var last = this._manager.branch.getCharPref("pinged");
  107.     var migrated = this._manager.branch.getCharPref("migrated");
  108.           
  109.     //use migrated value if last is blank
  110.     if (last == "" && migrated != "")
  111.       last = migrated;
  112.     
  113.     switch (last) {
  114.       case "0.8.5":
  115.         break;    
  116.       case "":
  117.       case "*":
  118.         this._open(ffIPing.OPEN_INSTALL, null, "0.8.5");
  119.         break;
  120.       default:       
  121.         this._open(ffIPing.OPEN_UPGRADE, last, "0.8.5");
  122.         break;
  123.     };
  124.     
  125.     //set pref
  126.     this._manager.branch.setCharPref("pinged", "0.8.5");
  127.   },
  128.   
  129.   _open: function(aType, aFromVer, aToVer)
  130.   { 
  131.     //create query url
  132.     var query = this._URL + "?type=" + aType;
  133.     switch (aType) {
  134.       case ffIPing.OPEN_UNINSTALL:
  135.         query = query + "&from=" + aFromVer;
  136.         break;
  137.       case ffIPing.OPEN_UPGRADE:
  138.         query = query + "&from=" + aFromVer + "&to=" + aToVer;
  139.         break;
  140.       case ffIPing.OPEN_INSTALL:
  141.         query = query + "&to=" + aToVer;
  142.         break;
  143.     };
  144.  
  145.     query = query + "&guid=" + this._manager.guid; 
  146.     
  147.     //send ping request    
  148.     var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);      
  149.     request.open("GET", query, true);
  150.     try {
  151.       if (request.channel instanceof Components.interfaces.nsISupportsPriority)
  152.         request.channel.priority = Components.interfaces.nsISupportsPriority.PRIORITY_HIGHEST;
  153.     } catch(e) {};
  154.     request.send(null);
  155.   },
  156.  
  157.   _dsObserve: function(aDS, aSource, aProperty, aTarget)
  158.   {    
  159.     if (aSource.ValueUTF8 != "urn:mozilla:extension:" + "{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}")
  160.       return;
  161.  
  162.     var prefix = "http://www.mozilla.org/2004/em-rdf#";
  163.     var action = aProperty.ValueUTF8.replace(prefix, "");
  164.     var pinged = null;
  165.     switch (action) {
  166.       case "toBeUninstalled":
  167.         this._open(ffIPing.OPEN_UNINSTALL, "0.8.5", null); 
  168.         pinged = "*";
  169.         break;
  170.       case "toBeInstalled":
  171.         this._downgrade = true;
  172.       case "version":
  173.         if (this._downgrade) {
  174.           var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
  175.           var item = em.getItemList("{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}", Components.interfaces.nsIUpdateItem.TYPE_EXTENSION, {})[0];
  176.           try {
  177.             var vc = Components.classes["@mozilla.org/updates/version-checker;1"].getService(Components.interfaces.nsIVersionChecker);
  178.           } catch(e) {
  179.             vc = Components.classes["@mozilla.org/xpcom/version-comparator;1"].getService(Components.interfaces.nsIVersionComparator);    
  180.           };
  181.           if (vc.isValidVersion(item.version)) {
  182.             if ((vc.compare(item.version, "0.7.1") <= 0) && (vc.compare(item.version, "0.5.9") > 0)) {
  183.               pinged = item.version;
  184.               this._open(ffIPing.OPEN_UPGRADE, "0.8.5", pinged);
  185.             } else if (vc.compare(item.version, "0.5.9") <= 0) {
  186.               // if it is a downgrade to a version we do not track, then report it as an uninstall
  187.               this._open(ffIPing.OPEN_UNINSTALL, "0.8.5", null);
  188.               pinged = "*";
  189.             };
  190.           };
  191.         };
  192.         break;
  193.     };
  194.     
  195.     if (pinged)
  196.       this._manager.branch.setCharPref("pinged", pinged);
  197.   },
  198.   
  199.   ////////////////////////////
  200.   // nsIObserver  
  201.   observe: function(aSubject, aTopic, aData) 
  202.   {
  203.     var item = aSubject.QueryInterface(Components.interfaces.nsIUpdateItem);
  204.     if (item.id != "{0538E3E3-7E9B-4d49-8831-A227C80A7AD3}")
  205.       return;
  206.       
  207.     var pinged = null;
  208.     switch (aData) {
  209.       case "item-uninstalled":
  210.         this._open(ffIPing.OPEN_UNINSTALL, "0.8.5", null); 
  211.         pinged = "*";
  212.         break;
  213.       case "item-upgraded":
  214.         try {
  215.           var vc = Components.classes["@mozilla.org/updates/version-checker;1"].getService(Components.interfaces.nsIVersionChecker);
  216.         } catch(e) {
  217.           vc = Components.classes["@mozilla.org/xpcom/version-comparator;1"].getService(Components.interfaces.nsIVersionComparator);    
  218.         };
  219.         if (vc.isValidVersion(item.version) && (vc.compare(item.version, "0.7.1") <= 0)) {
  220.           pinged = item.version;
  221.           this._open(ffIPing.OPEN_UPGRADE, "0.8.5", pinged);
  222.         };
  223.         break;
  224.     };
  225.     
  226.     if (pinged)
  227.       this._manager.branch.setCharPref("pinged", pinged);
  228.   },
  229.     
  230.   ///////////////////////////
  231.   // nsIRDFObserver
  232.   onAssert: function(aDS, aSource, aProperty, aTarget) { this._dsObserve(aDS, aSource, aProperty, aTarget); },  
  233.   onUnassert: function(aDS, aSource, aProperty, aTarget) {},
  234.   onChange: function(aDS, aSource, aProperty, aOldTarget, aNewTarget) { this._dsObserve(aDS, aSource, aProperty, aNewTarget); },  
  235.   onMove: function(aDS, aOldSource, aNewSource, aProperty, aTarget) {},
  236.   onBeginUpdateBatch: function(aDS) {},
  237.   onEndUpdateBatch: function(aDS) {},
  238.      
  239.   ///////////////////////////
  240.   // nsIClassInfo  
  241.   getInterfaces: function(aCount)
  242.   {
  243.     var ifaces = new Array();
  244.     ifaces.push(Components.interfaces.ffIPing);
  245.     ifaces.push(Components.interfaces.nsIRDFObserver);
  246.     ifaces.push(Components.interfaces.nsIObserver);
  247.     ifaces.push(Components.interfaces.nsIClassInfo);
  248.     ifaces.push(Components.interfaces.nsISupports);
  249.     aCount.value = ifaces.length;
  250.     return ifaces;
  251.   },
  252.   
  253.   getHelperForLanguage: function(aLanguage) { return null; },
  254.   get contractID() { return CONTRACT_ID; },
  255.   get classID() { return CLASS_ID; },
  256.   get classDescription() { return CLASS_NAME; },
  257.   get implementationLanguage() { return Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT; },
  258.   get flags() { return Components.interfaces.nsIClassInfo.SINGLETON; },
  259.          
  260.   ///////////////////////////
  261.   // nsISupports
  262.   QueryInterface: function (aIID)
  263.   {
  264.     if (!aIID.equals(Components.interfaces.ffIPing) &&
  265.         !aIID.equals(Components.interfaces.nsIRDFObserver) &&
  266.         !aIID.equals(Components.interfaces.nsIObserver) &&
  267.         !aIID.equals(Components.interfaces.nsIClassInfo) &&      
  268.         !aIID.equals(Components.interfaces.nsISupports))
  269.       throw Components.results.NS_ERROR_NO_INTERFACE;
  270.     return this;
  271.   }
  272. };
  273.  
  274. /******************************************************************************
  275.  * XPCOM Functions for construction and registration
  276.  ******************************************************************************/
  277. var gModule = {
  278.   _firstTime: true,
  279.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  280.   {
  281.     if (this._firstTime) {
  282.       this._firstTime = false;
  283.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  284.     };
  285.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  286.     aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  287.   },
  288.  
  289.   unregisterSelf: function(aCompMgr, aLocation, aType)
  290.   {
  291.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  292.     aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);        
  293.   },
  294.   
  295.   getClassObject: function(aCompMgr, aCID, aIID)
  296.   {
  297.     if (!aIID.equals(Components.interfaces.nsIFactory))
  298.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  299.  
  300.     if (aCID.equals(CLASS_ID))
  301.       return gFactory;
  302.  
  303.     throw Components.results.NS_ERROR_NO_INTERFACE;
  304.   },
  305.  
  306.   canUnload: function(aCompMgr) { return true; }
  307. };
  308.  
  309. var gFactory = {
  310.   createInstance: function (aOuter, aIID)
  311.   {
  312.     if (aOuter != null)
  313.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  314.     return (new ffPing()).QueryInterface(aIID);
  315.   }
  316. };
  317.  
  318. function NSGetModule(aCompMgr, aFileSpec) { return gModule; }